package com.coderman.infosys.auth.app.facadeimpl.system;

import com.coderman.infosys.auth.api.dto.system.ModuleDTO;
import com.coderman.infosys.auth.api.dto.system.SystemDTO;
import com.coderman.infosys.auth.api.dto.system.request.UpdateModuleRequestDTO;
import com.coderman.infosys.auth.app.convert.ModuleConvert;
import com.coderman.infosys.auth.app.convert.SystemConvert;
import com.coderman.infosys.auth.domain.bo.system.ModuleBO;
import com.coderman.infosys.auth.domain.bo.system.SystemBO;
import com.coderman.infosys.auth.domain.enums.errors.ModuleErrorEnum;
import com.coderman.infosys.auth.domain.factory.SystemFactory;
import com.coderman.infosys.auth.domain.gataway.SystemModuleQueryGataWay;
import com.coderman.infosys.auth.domain.repository.SystemModuleRepository;
import com.coderman.infosys.plat.auth.common.dto.PageDTO;
import com.coderman.infosys.plat.auth.common.dto.ResultDataDto;
import com.coderman.infosys.plat.auth.common.page.PageBO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.coderman.infosys.auth.api.facade.system.ModuleFacade;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @Description:模块或者菜单接口实现类
 * @Author：
 * @CreateTime：2022-01-02 15:27:39
 * @version v1.0
 */
@Service
public class ModuleFacadeImpl   implements ModuleFacade{

	private  Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SystemModuleRepository systemModuleRepository;

	@Autowired
	private SystemFactory systemFactory;

	@Autowired
	private SystemModuleQueryGataWay systemModuleQueryGataWay;


	@Override
	@Transactional(rollbackFor = Exception.class)
	public ResultDataDto<Boolean> registModule(ModuleDTO moduleDTO) {
		ModuleBO moduleBO = ModuleConvert.INSTANCE.dto2bo(moduleDTO);
		SystemBO systemBO = systemFactory.initSystemByModule(moduleBO);
		systemModuleRepository.registSystemMeta(systemBO);
		return ResultDataDto.success(true);
	}

	@Override
	public ResultDataDto<PageDTO> getPageList(PageDTO pageDTO) {
		PageBO pageBO = pageDTO.convertToBO();
		List<ModuleBO> moduleBOList = systemModuleQueryGataWay.queryModulePage(pageBO);
		List<ModuleDTO> moduleDTOList = ModuleConvert.INSTANCE.moduleBOs2dtoList(moduleBOList);
		return ResultDataDto.success(pageDTO.initData(moduleDTOList,pageBO.getTotalRows()));
	}

	@Override
	public ResultDataDto<ModuleDTO> getByModuleCode(String moduleCode) {

		ModuleBO moduleBO = systemModuleRepository.getByModuleCode(moduleCode);
		if(moduleBO == null){
			return ResultDataDto.fail(ModuleErrorEnum.BATCH_INSERT_ERROR);
		}
		return ResultDataDto.success(ModuleConvert.INSTANCE.bo2dto(moduleBO));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public ResultDataDto<Boolean> updateMenu(UpdateModuleRequestDTO requestDTO) {
		ModuleBO moduleBO = ModuleConvert.INSTANCE.dto2bo(requestDTO);
		SystemBO systemBO = systemFactory.initSystemByModule(moduleBO);
		systemModuleRepository.updateSystemMeta(systemBO);
		return ResultDataDto.success(true);
	}
}